﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using UFSSWebApp.UFSSservicereference;

namespace UFSSWebApp
{
    public partial class _Default : System.Web.UI.Page
    {
        UFSSserviceSoapClient ussc = new UFSSserviceSoapClient();
        UserCreds ucreds = new UserCreds();
        byte[] file = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            ucreds.Username = "robuser";
            ucreds.Password = "pass";
            ussc.LoginUser(ref ucreds);
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            ussc.DownloadFile(ucreds, 40, ref file);
            FileReceiver fr = new FileReceiver("test.png", file);
            //fr.WriteFile();
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            FileReceiver fr = new FileReceiver();
            fr.retrieveFile(ucreds, 400);
            HttpResponse resp = fr.retrieveFile(ucreds, 400);
            // retrieveFile(ucreds, 400);
        }

        public bool retrieveFile(UFSSservicereference.UserCreds uc, int fileid)
        {
            UFSSservicereference.UFSSFile fileinfo;
            byte[] file = null;

            fileinfo = ussc.getFileInfo(uc, fileid);
            ussc.DownloadFile(uc, fileid, ref file);
            FileReceiver fr = new FileReceiver(fileinfo.filename, file);
            string path = fileinfo.hash + "\\" + fileinfo.filename;
            fr.WriteFile(path);
            Response.Buffer = true;
            Response.Clear();
            Response.ClearContent();
            Response.ClearHeaders();
            Response.ContentType = "application/octet-stream";
            Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileinfo.filename);
            string mappath = Server.MapPath(path);
            Response.TransmitFile(Directory.GetCurrentDirectory() + "\\" + path);
            Response.End();
            return true;
        }
    }
}